package com.guogee.pdb.dao;

import com.guogee.pdb.bean.FamilyDevice;
import com.guogee.pdb.db.DBHelper.FamilyDevicesCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

public class FamilyDeviceDAO extends ADao<FamilyDevice> {

	public FamilyDeviceDAO() {
		dbCollection = dbHelper.getIsmartDB().getCollection(FamilyDevicesCollection.COLLECTION_NAME);
		if(dbCollection.getIndexInfo() == null){
			dbCollection.createIndex(new BasicDBObject(FamilyDevicesCollection.FAMILY_ID,-1));
		}
	}

	@Override
	public boolean insertItem(FamilyDevice obj) {
		if(insertItem1(obj)!=null){
			return true;
		}
		return false;
	}

	@Override
	public FamilyDevice parstItemFromCursor(DBCursor cursor) {
		return parstDBObject(cursor.next());
	
	}

	@Override
	public String insertItem1(FamilyDevice obj) {
		// TODO Auto-generated method stub
		DBObject dbObj = new BasicDBObject();
		dbObj.put(FamilyDevicesCollection.FAMILY_ID, obj.getFamilyId());
		dbObj.put(FamilyDevicesCollection.DEV_ID, obj.getDevId() == null ? "" : obj.getDevId());
		dbObj.put(FamilyDevicesCollection.DEV_TYPE, obj.getDevType());
		dbObj.put(FamilyDevicesCollection.DEV_VERSION, obj.getDevVersion());
		dbObj.put(FamilyDevicesCollection.DEV_MAC, obj.getDevMac());
		
		if(checkWritenResult(dbCollection.insert(dbObj))){
			return dbObj.get(FamilyDevicesCollection.ID).toString();
		}
		return null;
	}

	@Override
	public FamilyDevice parstDBObject(DBObject object) {
		// TODO Auto-generated method stub
		FamilyDevice obj = new FamilyDevice();
		obj.setId(object.get(FamilyDevicesCollection.ID).toString());
		obj.setFamilyId(object.get(FamilyDevicesCollection.FAMILY_ID).toString());
		obj.setDevId(object.get(FamilyDevicesCollection.DEV_ID).toString());
		obj.setDevType(Integer.valueOf(object.get(FamilyDevicesCollection.DEV_TYPE).toString()));
		obj.setDevVersion(Integer.valueOf(object.get(FamilyDevicesCollection.DEV_VERSION).toString()));
		obj.setDevMac(object.get(FamilyDevicesCollection.DEV_MAC).toString());
		return obj;
	}
}
